package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.mapper.SensitiveMapper;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.service.SensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class SensitiveServiceImpl extends BaseServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {

        if (dto == null) return paramError;
        Page page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        return selectFindAll(dto, page, queryWrapper);
    }


    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive==null)return paramError;
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return okResult;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null || adSensitive.getId()==null){
            return paramError;
        }
        updateById(adSensitive);
        return okResult;
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null)return paramError;
        AdSensitive byId = getById(id);
        if(byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return okResult;
    }
}
